package list.answer;

import list.ListNode;

import java.util.Stack;

/**
 * @Author wy
 * @Description TODO
 * @Date 2023/6/7 14:07
 * @Version 1.0
 */
public class List01_List {

    public static ListNode ReverseList(ListNode head) {
        //新链表
        ListNode pre=null;
        ListNode next=null;

        ListNode current=head;
        while( current!=null){
            //1、下一个next先走
            next=current.next;
            //2、当前节点.next与后面断点，指向前面的节点
            current.next=pre;
            //3、pre往后面，指向当前的current
            pre=current;
            //4、current与next同一个位置
            current=next;
        }
        return pre;


    }

    public static void main(String[] args) {
        ListNode header=new ListNode(1);
        ListNode two=new ListNode(2);
        ListNode three=new ListNode(3);
        //1-->2
        header.next=two;
        //2-->3
        //header.next.next=three;
        two.next=three;

        System.out.println("----原链表顺序-------");
        show(header);

        System.out.println("\n---反转后的结果------");
        ListNode newNode=ReverseList(header);
        show(newNode);
    }
    private static void show(ListNode head){
        if(head==null){
            System.out.println("空链表");
        }else{
            ListNode temp=head;
            while(temp!=null){
                System.out.print(temp.val+"\t");
                //移后移动一个
                temp=temp.next;
            }
            System.out.println();
        }
    }
}
